package com.mall.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.mapper.SubjectMapper;
import com.mall.model.domain.CmsSubject;
import com.mall.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 根据专题名称分页获取商品专题
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResult querySubjectByPage(Integer pageNum, Integer pageSize, String keyword) {
        //构建分页对象
        Page<CmsSubject> page = new Page<>();
        //构建查询商品专题的queryWrapper
        LambdaQueryWrapper<CmsSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isEmpty(keyword), CmsSubject::getTitle, keyword);
        Page<CmsSubject> selectPage = subjectMapper.selectPage(page, queryWrapper);
        //构建返回的分页数据
        return new PageResult(pageNum, pageSize, (int) selectPage.getTotal(), selectPage.getRecords());
    }


    /**
     * 获取全部商品专题
     *
     * @return
     */
    public List<CmsSubject> queryAllSubject() {
        //构建查询商品专题的queryWrapper
        LambdaQueryWrapper<CmsSubject> queryWrapper = new LambdaQueryWrapper<>();
        //调用subjectMapper查询全部商品专题数据列表  返回数据
        return subjectMapper.selectList(queryWrapper);
    }
}
